// ==UserScript==
// @name         New Userscript
// @namespace    http://tampermonkey.net/
// @version      2024-11-28
// @description  try to take over the world!
// @author       You
// @match        https://www.baidu.com/*

// @run-at       document-start
// @grant        unsafeWindow
// ==/UserScript==

(function () {
    'use strict';
    var link = document.createElement('link');
    link.rel = 'stylesheet';
    link.type = 'text/css';
    link.href = 'https://www.layuicdn.com/layui/css/layui.css';
    let script = document.createElement("script");
    script.type = "text/javascript";
    script.src="https://www.layuicdn.com/layui/layui.js";
    document.head.appendChild(script);
    document.head.appendChild(link);
    window.addEventListener('load', addPanel)

    var cssText = `
    #MyButton {
        color: #592A0E;
        background: #fff;
        border: none;
        padding: 10px 20px;
        display: inline-block;
        font-size: 11px;
        font-weight: 400;
        width: 70x;
        text-transform: uppercase;
        cursor: pointer;
        position: fixed;
        top: 50%;
        left: -40px;
        z-index: 999;
        transition: all 0.7s;
    }

    #MyButton span {
        display: inline-block;
    }

    #MyButton::before {
        content: '';
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        right: 100%;
        background-image: linear-gradient(to right, #ff8177 0%, #ff867a 0%, #ff8c7f 21%, #f99185 52%, #cf556c 78%, #b12a5b 100%);
        opacity: 0;
        z-index: -1;
        transition: all 0.7s;
        backface-visibility: hidden;
    }

    #MyButton:hover {
        overflow: hidden;
        left: -10px;
        color: #fff;
        transition: all 0.7s;
    }

    #MyButton:hover::before {
        left: 0;
        right: 0;
        opacity: 1;
    }

    #MyButton:active {
        letter-spacing: 1.5px;
        box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
        transition: 100ms;
    }

    #MyButton:active::before {
        background-image: linear-gradient(to right, #e06a6a 0%, #e06d6d 0%, #e07373 21%, #d27878 52%, #ae4c4c 78%, #902525 100%);
        opacity: 1;
    }
    #tableBtnContainer{
        text-align: right;
    }
    `
    GMaddStyle(cssText);




    function addPanel() {
        // 定义表格数据
        //默认按照时间戳降序排序 +荣誉等级 粉丝牌名
        var tableData = [
            {name:'一个数学老师', content:'能再看下阵容吗', time:'2023-03-01 12:00:00', reply_is_mystery:'false', score:3890, level:15, uid:417503337, guard_level:'舰长', consumer_level:0, medal:"2"},
            {name:'二个数学老师', content:'能再看下阵容吗', time:'2023-04-01 12:00:00', reply_is_mystery:'false', score:3890, level:15, uid:417503337, guard_level:'无', consumer_level:25, medal:"2"},
            {name:'一个数学老师', content:'能再看下阵容吗', time:'2023-04-02 12:00:00', reply_is_mystery:'false', score:3890, level:15, uid:417503337, guard_level:'舰长', consumer_level:12, medal:"6"},
            {name:'二个数学老师', content:'能再看下阵容吗', time:'2023-03-01 23:00:00', reply_is_mystery:'false', score:3890, level:15, uid:417503337, guard_level:'总督', consumer_level:31, medal:"8"}
        ];
        var tableData01 = [
            {name:'一个数学老师', content:'能再看下阵容吗', time:'2023-03-01 12:00:00', reply_is_mystery:'false', score:3890, level:15, uid:417503337, guard_level:'舰长', consumer_level:0, medal:"2"},
            {name:'二个数学老师', content:'能再看下阵容吗', time:'2023-04-01 12:00:00', reply_is_mystery:'false', score:3890, level:15, uid:417503337, guard_level:'无', consumer_level:25, medal:"2"},
            {name:'一个数学老师', content:'能再看下阵容吗', time:'2023-04-02 12:00:00', reply_is_mystery:'false', score:3890, level:15, uid:417503337, guard_level:'舰长', consumer_level:12, medal:"6"},
            {name:'二个数学老师', content:'能再看下阵容吗', time:'2023-03-01 23:00:00', reply_is_mystery:'false', score:3890, level:15, uid:417503337, guard_level:'总督', consumer_level:31, medal:"8"}
        ];
        var timer;
        function genButtonSpan(text, foo, id, fooParams = {}) {
            let b = document.createElement('button');
            let textSpan = document.createElement('span');
            textSpan.textContent = text;
            b.appendChild(textSpan);
            // 使用箭头函数创建闭包来保存 fooParams 并传递给 foo
            b.addEventListener('click', () => {
                foo.call(b, ...Object.values(fooParams)); // 使用 call 方法确保 this 指向按钮对象
            });
            if (id) { b.id = id };
            return b;
        }

        function genButton(text, foo, id, fooParams = {}) {
            let b = document.createElement('button');
            b.textContent = text;
            // 使用箭头函数创建闭包来保存 fooParams 并传递给 foo
            b.addEventListener('click', () => {
                foo.call(b, ...Object.values(fooParams)); // 使用 call 方法确保 this 指向按钮对象
            });
            if (id) { b.id = id };
            return b;
        }

        function genTable(cls, id) {
            let tb = document.createElement('table');
            if (cls) { tb.className = cls };
            if (id) { tb.id = id };
            return tb;
        }

        function genDiv(cls, id) {
            let d = document.createElement('div');
            if (cls) { d.className = cls };
            if (id) { d.id = id };
            return d;
        }
        async function openPanelFunc() {
            layer.open({
                type: 0, // page 层类型
                title: '选择数据类型',
                shade: 0,
                btn: ['弹幕', '礼物+SC+舰队', '其他'],
                // 按钮1 的回调
                btn1: btn1Fuction,
                btn2: btn2Fuction,
                btn3: function (index, layero, that) {
                    // 按钮3 的回调
                    // return false // 点击该按钮后不关闭弹层
                }
            });
        }

        function btn1Fuction(index, layero, that) {
            layui.use(['layer', 'table'], function () {
                var layer = layui.layer;
                var table = layui.table;

                // 使用 layer.open 打开一个新的弹出层
                layer.open({
                    type: 1, // 表示这是一个页面层
                    title: '各种弹幕/SC',
                    shade: 0,
                    shadeClose: true, // 点击遮罩关闭
                    area: ['40%', '40%'], // 设置宽度和高度
                    content: layui.$('#MyTableDiv'), // 显示隐藏的 div 内容
                    success: function (layero, index) {
                        // 在弹出层成功打开后渲染表格
                        renderTable(tableData);
                        // 设置定时器，每3秒更新一次数据并重新加载表格
                        timer = setInterval(function () {
                            updateTableData(tableData);
                            console.log("定时器--166")
                            table.reloadData('MyTable', { data: tableData });
                        }, 3000);
                        let tableBtn02 = document.querySelector('#tableBtn02')
                        if(tableBtn02.textContent == '开始'){
                            tableBtn02.className += ' layui-btn-warm';
                            tableBtn02.textContent = '暂停';
                        }
                    },
                    end: function () {

                        if (timer) {
                            clearInterval(timer);
                            timer = undefined; // 清除引用
                        }
                    }
                });
                // 渲染表格
                function renderTable(data) {
                    table.render({
                        id: 'MyTable',
                        elem: '#MyTable',
                        data: data,
                        cols: [[
                            //排序默认是字典序，符合要求
                            { field: 'time', title: '时间', width: 160, sort: true, fixed: 'left' },
                            { field: 'name', title: '姓名', width: 120 },
                            { field: 'content', title: '消息', width: 160},
                            { field: 'medal', title: '粉丝牌', width: 80},
                            { field: 'level', title: '粉丝牌LV', width: 100},
                            { field: 'consumer_level', title: '荣誉LV', width: 100},
                            { field: 'score', title: '贡献值', width: 100},
                            { field: 'guard_level', title: '身份', width: 80},
                            { field: 'reply_is_mystery', title: '匿名', width: 80},
                            { field: 'uid', title: 'uid', width: 80}
                        ]],
                        page: true // 不显示分页
                    });
                }
            });

            return true;
        }
        function btn2Fuction(index, layero, that) {
            layui.use(['layer', 'table'], function () {
                var layer = layui.layer;
                var table = layui.table;

                // 使用 layer.open 打开一个新的弹出层
                layer.open({
                    type: 1, // 表示这是一个页面层
                    title: '各种弹幕/SC',
                    shade: 0,
                    shadeClose: true, // 点击遮罩关闭
                    area: ['40%', '40%'], // 设置宽度和高度
                    content: layui.$('#MyTableDiv01'), // 显示隐藏的 div 内容
                    success: function (layero, index) {
                        // 在弹出层成功打开后渲染表格
                        renderTable(tableData01);
                        // 设置定时器，每3秒更新一次数据并重新加载表格
                        timer = setInterval(function () {
                            updateTableData(tableData01);
                            console.log("定时器--166")
                            table.reloadData('MyTable', { data: tableData01 });
                        }, 3000);
                    },
                    end: function () {
                        if (timer) {
                            clearInterval(timer);
                            timer = undefined; // 清除引用
                        }
                    }
                });
                // 渲染表格
                function renderTable(data) {
                    table.render({
                        id: 'MyTable01',
                        elem: '#MyTable01',
                        data: data,
                        cols: [[
                            //排序默认是字典序，符合要求
                            { field: 'time', title: '时间', width: 160, sort: true, fixed: 'left' },
                            { field: 'name', title: '姓名', width: 120 },
                            { field: 'content', title: '消息', width: 160},
                            { field: 'medal', title: '粉丝牌', width: 80},
                            { field: 'level', title: '粉丝牌LV', width: 100},
                            { field: 'consumer_level', title: '荣誉LV', width: 100},
                            { field: 'score', title: '贡献值', width: 100},
                            { field: 'guard_level', title: '身份', width: 80},
                            { field: 'reply_is_mystery', title: '匿名', width: 80},
                            { field: 'uid', title: 'uid', width: 80}
                        ]],
                        page: true // 不显示分页
                    });
                }
            });

            return false;
        }
        function btn3Fuction(index, layero, that) {
            layer.open({
                type: 1, // 表示这是一个页面层
                title: '其他',
                shade: 0,
                shadeClose: true, // 点击遮罩关闭
                area: ['40%', '40%'], // 设置宽度和高度
                content: layui.$('#MyTableDiv01'), // 显示隐藏的 div 内容
                success: function (layero, index) {

                    timer = setInterval(function () {
                        updateTableData(tableData01);
                    }, 3000);
                },
                end: function () {
                    if (timer) {
                        clearInterval(timer);
                        timer = undefined; // 清除引用
                    }
                }
            });
        }
        // 更新表格数据
        function updateTableData(data) {
            data.forEach(function (item) {
                item.uid += 1; // 每个年龄增加1
            });
        }
        function tableBtn01Fuc() {
            layui.use(['layer', 'table'], function () {
                var layer = layui.layer;
                var table = layui.table;
                table.exportFile('MyTable',null,{
                    title: '弹幕-SC' + Date.now().toString()
                });
            });

        }
        function tableBtn02Fuc() {
            console.log(timer)
            let tableBtn02 = document.querySelector('#tableBtn02')
            if (timer) {
                clearInterval(timer);
                timer = undefined;
                tableBtn02.classList.remove('layui-btn-warm');
                tableBtn02.textContent = '开始';
                return;
            }
            layui.use(['layer', 'table'], function () {
                var layer = layui.layer;
                var table = layui.table;
                timer = setInterval(function () {
                    updateTableData(tableData);
                    console.log("定时器--166")
                    table.reloadData('MyTable01', { data: tableData });
                }, 3000);
                tableBtn02.className += ' layui-btn-warm';
                tableBtn02.textContent = '暂停';
            })
            return true;
        }
        let myButton = genButtonSpan('crawler', openPanelFunc, 'MyButton');
        let myTable = genTable('layui-hide', 'MyTable');
        let myTableDiv = genDiv(undefined, 'MyTableDiv');
        let myTable01 = genTable('layui-hide', 'MyTable01');
        let myTableDiv01 = genDiv(undefined, 'MyTableDiv01');
        let tableBtnContainer = genDiv('layui-btn-container', 'tableBtnContainer');
        let tableBtn01 = genButton('导出', tableBtn01Fuc, 'tableBtn01');
        let tableBtn02 = genButton('暂停', tableBtn02Fuc, 'tableBtn02');
        tableBtn01.className = 'layui-btn';
        tableBtn01.className += ' layui-btn-sm';
        tableBtn02.className = 'layui-btn';
        tableBtn02.className += ' layui-btn-sm';
        tableBtn02.className += ' layui-btn-warm';

        tableBtnContainer.appendChild(tableBtn01);
        tableBtnContainer.appendChild(tableBtn02);
        myTableDiv.appendChild(tableBtnContainer);
        myTableDiv.appendChild(myTable);
        myTableDiv01.appendChild(myTable01);

        document.body.appendChild(myButton);
        document.body.appendChild(myTableDiv);
        document.body.appendChild(myTableDiv01);
    }

    function GMaddStyle(css) {
        var myStyle = document.createElement('style');
        myStyle.textContent = css;
        var doc = document.head || document.documentElement;
        doc.appendChild(myStyle);
    }
})();